<?php
/**
**韵达API对接
**/
class YundaAPIAction extends CommonAction{
    
  /**
  **向韵达请求数据,并返回相应 的xml结果
  **@xmldata 传送的xml数据
  **@interface_id 要使用的接口  1 为下单,2修改订单,3 取消订单,4查询 订单 5 查询 运输状态
  **return xml data
  **/
  public function askYunda($xmldata,$interface_id = 1){
     $interface = $this->getInterfaceUrl($interface_id); 
        $url="http://orderdev.yundasys.com:10209/cus_order/order_interface/".$interface;
		$data=$this->vfunction($xmldata,$interface_id);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);      // set url to post to
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);      // times out 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     // add POST fields
		$result = curl_exec($ch); 
		if($error = curl_error($ch)) 
		{   
			$result= $error;
			return -1;   
		}
        return $result; //return xml data


 }
    //TODO 韵达帐号使用配置参数
    /*生成指定格式的发送数据	外部系统接入*/
    private function make_send_data_in($xmldata,$request='data',$user='YUNDA',$pass='123456')
    {
        return ("partnerid=".$user."&version=1.0&request={$request}&xmldata=".urlencode(base64_encode($xmldata))."&validation=".urlencode(strtolower(md5(base64_encode($xmldata).$user.$pass))));
    }

    private function getInterfaceUrl($id = 0){
        $interface = "";
       switch ($id){
          case 1 : //create order
              $interface = "interface_receive_order__mailno.php";
              break;
          case 2: //modify order
              $interface = "interface_modify_order.php";
              break;
          case 3: //cancel order
              $interface = "interface_cancel_order.php";
              break;
          case 4: //query order
              $interface = "interface_order_info.php";
              break;
          case 5: //query transit status order
              $interface = "interface_transite_search.php";
              break;
          default:
              break;
       }    
       return $interface;
    }
/**
**集成简易化向韵达传递的数据
**@str 传递的xmldata
**@form_interface 使用的接口编号
**/
 public function vfunction($str,$form_interface){
    $yunda_account = C('yunda_account');
    $pass = C('yunda_pwd');
    
	$form_interface=(int) $form_interface;
	switch ($form_interface){
		case 1:
        case 2:
        case 4:
        case 5:
			$rt=$this->make_send_data_in($str,'data',$yunda_account,$pass);
			return $rt;
			break;
		case 3:
			$rt=$this->make_send_data_in($str,'cancel_order',$yunda_account,$pass);
			return $rt;
			break;
	}
}

/**
**根据订单数组返回XML数据
**@order 订单数组
**/
public static function buildOrderXMLData($order ){
    if(empty($order) || !is_array($order) )
        return false;
    $xmldata = " 
		   <order>
		    <order_serial_no>".$order['id']."</order_serial_no>
		    <khddh>".$order['sale_no']."</khddh>
		   	<nbckh>".$order['mall_no']."</nbckh>
	        <sender>
	            <name>隐形眼镜上海仓库</name>
	            <company>DYW隐形眼镜上海仓</company>
	            <city>上海市,上海市,普陀区</city>
	            <address>上海市,上海市,普陀区同普路1225弄1号3楼</address>
	            <postcode>200062</postcode>
	            <phone>021-62563719</phone>
	            <mobile></mobile>
	            <branch></branch>
	        </sender>
	        <receiver>
	            <name>".$order['shipping_name']."</name>
	            <company></company>
	            <city>".$order['province'].",".$order['city'].",".$order['county']."</city>
	            <address>".$order['province'].",".$order['city'].",".$order['county'].$order['address']."</address>
	            <postcode></postcode>
	            <phone>".$order['phone']."</phone>
	            <mobile>".$order['mobile']."</mobile>
	            <branch></branch>
	        </receiver>
	        <weight></weight>
	        <size></size>
	        <value></value>
	        <freight></freight>
	        <premium></premium>
	        <other_charges></other_charges>
	        <collection_currency></collection_currency>
	        <collection_value></collection_value>
	        <special></special>
	        <items>
	        	<item>
	                <name></name>
	                <number></number>
	                <remark></remark>
	            </item>
	         </items>
	        <remark>".$order['customer_msg']."</remark>
	        <cus_area1>订单号：".$order['sale_no']."</cus_area1>
	        <cus_area2></cus_area2>
	      </order>";
        return $xmldata;
}

}
?>
